import {Matrix} from "./matrix";
import {Fence} from "./fence";

class FenceGroup {
  spu
  sku_list = []
  fences

  constructor(spu) {
    this.spu = spu
    this.sku_list = spu.sku_list
  }
  setCellStatusById(cellId, status){
    this.eachCell((cell)=>{
      if(cell.id === cellId){
        cell.status = status
      }
    })
  }

  setCellStatusByXY(x, y, status){
    this.fences[x].cells[y].status = status
  }

  getDefaultSku(){
    const defaultSkuId = this.spu.default_sku_id
    if(!defaultSkuId){
      return
    }
    return this.sku_list.find(s=>s.id === defaultSkuId)
  }

  initFences() {
    const matrix = this._createMatrix(this.sku_list)
    const fences = []
    const AT = matrix.transpose()
    AT.forEach((r) => {
      const fence = new Fence(r)
      fence.init()
      fences.push(fence)
    })
    this.fences = fences
  }



  eachCell(cb) {
    for(let i=0;i<this.fences.length;i++){
      for(let j=0;j<this.fences[i].cells.length;j++){
        const cell = this.fences[i].cells[j]
        cb(cell, i, j)
      }
    }
  }

  _createMatrix(sku_list) {
    const m = [];
    sku_list.forEach((sku) => {
      m.push(sku.specs)
    })
    return new Matrix(m)
  }
}

export {
  FenceGroup
}
